/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.service.impl;

import com.wteam.modules.system.service.AreaService;
import com.wteam.modules.system.domain.Area;
import com.wteam.modules.system.domain.dto.AreaDTO;
import com.wteam.modules.system.domain.criteria.AreaQueryCriteria;
import com.wteam.modules.system.domain.mapper.AreaMapper;
import com.wteam.modules.system.repository.AreaRepository;
import com.wteam.exception.BadRequestException;
import com.wteam.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 地区 业务实现层.
 *
 * @author herry_wong
 * @since 2020-12-28
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "area")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class AreaServiceImpl implements AreaService {

    private final AreaRepository areaRepository;

    private final AreaMapper areaMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(AreaQueryCriteria criteria, Pageable pageable){
        Page<Area> page = areaRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(areaMapper::toDto));
    }

    @Override
    public List<AreaDTO> queryAll(AreaQueryCriteria criteria){
        return areaMapper.toDto(areaRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "'id:' + #p0")
    public AreaDTO findDTOById(Long id) {
        Area area = areaRepository.findById(id).orElse(null);
        ValidUtil.notNull(area, Area.ENTITY_NAME, "id", id);
        return areaMapper.toDto(area);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AreaDTO create(Area resources) {
        return areaMapper.toDto(areaRepository.save(resources));
    }

    @Override
    @CacheEvict(key = "'id:' + #p0.id")
    @Transactional(rollbackFor = Exception.class)
    public void update(Area resources) {
        Area area = areaRepository.findById(resources.getId()).orElse(null);
        ValidUtil.notNull(area, Area.ENTITY_NAME, "id",resources.getId());

        area.copy(resources);
        areaRepository.save(area);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Set<Long> ids) {
        redisUtils.delByKeys("area::id:", ids);
        areaRepository.logicDeleteInBatchById(ids);
    }

    @Override
    public void download(List<AreaDTO> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AreaDTO area : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建时间", area.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}